﻿using System;
using System.Configuration;
using System.Web;
using PTop.Code.Machine;

namespace PTop.Code
{
    public sealed class Licence
    {
        public static string GetHardwareInfo()
        {
            var hardware = new HardwareInfo();
            var cpuId = hardware.GetCpuID();
            var diskId = hardware.GetHardDiskID();
            var info = cpuId + diskId;
            var des = DESEncrypt.Encrypt(info, "fsd#4fs3");
            return des;
        }

        public static bool IsLicence()
        {
            var key = GetHardwareInfo();
            string host = HttpContext.Current.Request.Url.Host.ToLower();
            if (host.Equals("localhost"))
                return true;
            string licence = ConfigurationManager.AppSettings["LicenceKey"];
            if (licence != null && licence.ToUpper() == Md5.md5(key, 32).ToUpper())
                return true;
            return false;
        }

        public static string GetLicence()
        {
            var licence = Configs.GetValue("LicenceKey");
            if (string.IsNullOrEmpty(licence))
            {
                licence = Common.GuId();
                Configs.SetValue("LicenceKey", licence);
            }
            return Md5.md5(licence, 32);
        }
    }
}
